﻿using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Approval
{
    public class GetDepartMentRoleHandler : IRequestHandler<GetDepartMentRoleCommand, ApiResult<string>>
    {
        private readonly IBaseRepository<DepartmentModel> departmentRepo;
        private readonly IBaseRepository<UserModel> userRpeo;

        public GetDepartMentRoleHandler(IBaseRepository<DepartmentModel> departmentRepo,IBaseRepository<UserModel> userRpeo)
        {
            this.departmentRepo = departmentRepo;
            this.userRpeo = userRpeo;
        }
        public Task<ApiResult<string>> Handle(GetDepartMentRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<string> res = new ApiResult<string>();
            var info = departmentRepo.GetAll().FirstOrDefault(x => x.Id == request.DepartmentId).Uid;
            var user = userRpeo.GetAll().FirstOrDefault(x => x.Id == info).UserName;
            res.Code = ApiEnum.Success;
            res.Msg = "部门领导查询成功";
            res.Data = user.ToString();
            return Task.FromResult(res);
        }
    }
}
